using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ViewEvents : System.Web.UI.UserControl
{
    DataTable Events;
    protected void Page_Load(object sender, EventArgs e)
    {
        ManageEvent manager = new ManageEvent();
        String ID = Request.QueryString["id"];
        DataSet ds = manager.getAllEvents(Convert.ToInt32(ID));
        
        Events = ds.Tables[0];
    }
    protected void calEvents_DayRender(object sender, DayRenderEventArgs e)
    {
        DataRow[] rows = Events.Select(
         String.Format(
            "dateTime >= #{0}# AND dateTime < #{1}#",
            e.Day.Date.ToShortDateString(),
            e.Day.Date.AddDays(1).ToShortDateString()
         )
      );

        foreach (DataRow row in rows)
        {
            HyperLink day = new HyperLink();
            day.ID = "lnkDay";
            
            //day.runat = "server";
            day.Text = "\n [" +row["Title"].ToString().Trim() +"]";
            //day.ValidationGroup = "viewevents";
            day.NavigateUrl = Page.ClientScript.GetPostBackClientHyperlink(lblEvent, row["EventID"].ToString().Trim(), true);
            e.Cell.Controls.Add(day);            
        }
    }
    protected void lblEvent_Click(object sender, EventArgs e)
    {
        pnlCalendar.Visible = false;
        pnlEventDetails.Visible = true;

        int id = Convert.ToInt32(Request.Form["__EVENTARGUMENT"]);

        ManageEvent eManager = new ManageEvent();
        Event currentEvent = eManager.getEvent(id);

        ManagePerson pManager = new ManagePerson();
        Person contact = pManager.getPerson(currentEvent.ContactID);

        char[] c = contact.PhoneNumber.ToCharArray();

        lblTitleText.Text = currentEvent.Title;
        lblDateText.Text = currentEvent.dateTime.Date.ToString("d");
        lblTimeText.Text = currentEvent.dateTime.ToShortTimeString();
        lblLocationText.Text = currentEvent.Location;
        lblContactName.Text = contact.FirstName +" "+ contact.LastName;
        lblContactPhone.Text = "("+c[0]+c[1]+c[2]+") "+c[3]+c[4]+c[5]+"-"+c[6]+c[7]+c[8]+c[9];
        lblDescriptionText.Text = currentEvent.Description;
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        pnlCalendar.Visible = true;
        pnlEventDetails.Visible = false;
        pnlSearchResults.Visible = false;
    }
    protected void btnSearchEvents_Click(object sender, EventArgs e)
    {
        pnlCalendar.Visible = false;
        pnlSearchResults.Visible = true;

        ManageEvent manager = new ManageEvent();

        String ID = Request.QueryString["id"];

        lvSearchResults.DataSource = manager.findEvents(Convert.ToInt32(ID),txtEventKeyword.Text);
        lvSearchResults.DataBind();
    }
    protected void btnToDetails_Click(object sender, EventArgs e)
    {
        pnlCalendar.Visible = false;
        pnlSearchResults.Visible = false;
        pnlEventDetails.Visible = true;
        btnBackToSearch.Visible = true;

        Button btn = sender as Button;
        string cmdArg = btn.CommandArgument;
        int id = Convert.ToInt32(cmdArg);//Request.Form["__EVENTARGUMENT"]);

        ManageEvent eManager = new ManageEvent();
        Event currentEvent = eManager.getEvent(id);

        ManagePerson pManager = new ManagePerson();
        Person contact = pManager.getPerson(currentEvent.ContactID);

        char[] c = contact.PhoneNumber.ToCharArray();

        lblTitleText.Text = currentEvent.Title;
        lblDateText.Text = currentEvent.dateTime.Date.ToString("d");
        lblTimeText.Text = currentEvent.dateTime.ToString("HH:mm tt");
        lblLocationText.Text = currentEvent.Location;
        lblContactName.Text = contact.FirstName + " " + contact.LastName;
        lblContactPhone.Text = "(" + c[0] + c[1] + c[2] + ") " + c[3] + c[4] + c[5] + "-" + c[6] + c[7] + c[8] + c[9];
        lblDescriptionText.Text = currentEvent.Description;
    }
    protected void btnBackToSearch_Click(object sender, EventArgs e)
    {
        pnlCalendar.Visible = false;
        pnlSearchResults.Visible = true;
        pnlEventDetails.Visible = false;
        btnBackToSearch.Visible = false;
    }
}
